package com.arpa.wms.service.impl;

import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.wms.cancel.delivery.context.DeliveryCancelHandlerContext;
import com.arpa.wms.domain.consts.NumConst;
import com.arpa.wms.domain.entity.Delivery;
import com.arpa.wms.domain.enums.DeliveryStatusEnum;
import com.arpa.wms.service.DeliveryCancelHandleService;
import com.arpa.wms.service.IDeliveryService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 操作取消订单处理
 *
 * @author liuxr
 * @date 2021年05月17日 13:09
 */
@Service
@Log4j2
public class DeliveryCancelHandlerImpl implements DeliveryCancelHandleService {

    private final IDeliveryService deliveryService;

    private final DeliveryCancelHandlerContext deliveryCancelHandlerContext;

    public DeliveryCancelHandlerImpl(IDeliveryService deliveryService,
                                     DeliveryCancelHandlerContext deliveryCancelHandlerContext) {
        this.deliveryService = deliveryService;
        this.deliveryCancelHandlerContext = deliveryCancelHandlerContext;
    }

    /**
     * 根据波次号处理取消订单
     *
     * @param wavePickingCode 波次号
     * @author liuxr
     * @date 2021/5/17 11:06
     */
    @Override
    public Set<String> deliveryOrderCancel(String wavePickingCode) {
        //更新订单状态
        List<Delivery> deliveries = deliveryService.list(Wrappers.lambdaQuery(Delivery.class)
                .select(Delivery::getId,
                        Delivery::getCode,
                        Delivery::getStatus,
                        Delivery::getWavePickingCode,
                        Delivery::getWarehouseCode,
                        Delivery::getDistributionNum)
                .eq(Delivery::getWavePickingCode,wavePickingCode)
                .eq(Delivery::getGroupCode,UserUtil.getBranchCode())
                .eq(Delivery::getIsCancel, NumConst.NUM_ONE)
        );
        log.info("获取待取消订单列表：{}", JSONUtil.toJsonStr(deliveries));
        if (IterUtil.isNotEmpty(deliveries)) {
            deliveryOrderCancelHandle(deliveries);
        }
        return deliveries.stream().map(Delivery::getCode).collect(Collectors.toSet());
    }

    /**
     * 根据订单号处理取消订单
     *
     * @param deliveryCodes 订单号集合
     * @author liuxr
     * @date 2021/5/17 11:06
     */
    @Override
    public Set<String> deliveryOrderCancel(List<String> deliveryCodes) {
        //更新订单状态
        List<Delivery> deliveries = deliveryService.list(Wrappers.lambdaQuery(Delivery.class)
                .select(Delivery::getId,
                        Delivery::getGroupCode,
                        Delivery::getCode,
                        Delivery::getStatus,
                        Delivery::getWavePickingCode,
                        Delivery::getWarehouseCode,
                        Delivery::getDistributionNum)
                .in(Delivery::getCode,deliveryCodes)
                .eq(Delivery::getGroupCode, UserUtil.getBranchCode())
                .eq(Delivery::getIsCancel, NumConst.NUM_ONE)
        );
        log.info("获取待取消订单列表：{}", JSONUtil.toJsonStr(deliveries));
        if (IterUtil.isNotEmpty(deliveries)) {
            deliveryOrderCancelHandle(deliveries);
        }
        return deliveries.stream().map(Delivery::getCode).collect(Collectors.toSet());
    }

    /**
     * 处理取消订单
     *
     * @param warehouseCode 仓库标识
     * @param groupCode     所属机构
     * @author liuxr
     * @date 2021/12/9 16:03
     */
    @Override
    public void deliveryOrderCancel(String warehouseCode, String groupCode,List<String> shipmentCodes) {
        //更新订单状态
        List<Delivery> deliveries = deliveryService.list(Wrappers.lambdaQuery(Delivery.class)
                .select(Delivery::getId,
                        Delivery::getCode,
                        Delivery::getStatus,
                        Delivery::getWarehouseCode,
                        Delivery::getWavePickingCode,
                        Delivery::getDistributionNum)
                .ne(Delivery::getStatus, DeliveryStatusEnum.VOID.getValue())
                .in(Delivery::getShipmentCode, shipmentCodes)
                .eq(Delivery::getIsCancel, NumConst.NUM_ONE)
                .eq(Delivery::getWarehouseCode, warehouseCode)
                .eq(Delivery::getGroupCode, groupCode)
        );


        log.info("获取待取消订单列表：{}", JSONUtil.toJsonStr(deliveries));
        if (IterUtil.isNotEmpty(deliveries)) {
            deliveryOrderCancelHandle(deliveries);
        }

    }


    /**
     * 订单取消处理
     *
     * @param deliveryList 订单号
     * @author liuxr
     * @date 2021/11/23 10:12
     */
    @Transactional(rollbackFor = Exception.class)
    public void deliveryOrderCancelHandle(List<Delivery> deliveryList) {
        log.info("订单取消处理，已取消的订单:{}", JSONUtil.toJsonStr(deliveryList));
        deliveryList = deliveryList.stream()
                .filter(r -> !StrUtil.equals(r.getStatus(), DeliveryStatusEnum.VOID.getValue()))
                .collect(Collectors.toList());
        log.info("订单取消处理，需要处理的订单:{}", JSONUtil.toJsonStr(deliveryList));
        for (Delivery delivery : deliveryList) {
            deliveryCancelHandlerContext.getMethodResult(deliveryService,DeliveryStatusEnum.getEnumByValue(delivery.getStatus()).name(),delivery);
        }
    }
}
